import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login'
import Layout from '../views/layout'
import { getToken } from '../utils/token'
import Chart from '../views/layout/Chart'
import User from '../views/layout/User'
import Enterprise from '../views/layout/Enterprise'
import Question from '../views/layout/Question'
import Subject from '../views/layout/Subject'

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/layout',
      component: Layout,
      children: [
        { path: 'chart', component: Chart },
        { path: 'user', component: User },
        { path: 'enterprise', component: Enterprise },
        { path: 'question', component: Question },
        { path: 'subject', component: Subject }
      ]
    }
  ]
})

const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
